const app = getApp();
Page({
  data: {
    userInfo: {},
  },
  onLoad() {
    this.getUserInfo();
  },
  formSubmit(e) {
    let { name, identity_card } = this.data.userInfo;
    let { phone = '' } = e.detail.value;
    let isPass = true;
    let hint = '';
    if (phone === '') {
      hint = '请填写手机号码';
    } else if (!(/^(0|86|17951)?(13[0-9]|15[012356789]|16[6]|17[12345678]|18[0-9]|14[56789]|19[89])[0-9]{8}$/.test(phone))) {
      hint = '手机号格式不正确';
    } else {
      isPass = false;
      app.util.request({
        url: 'entry/wxapp/user',
        data: {
          phone,
          name,
          identity_card,
          op: 'postRealName',
        },
        module: 'cm_video_alarm',
        success: res => {
          wx.showModal({
            title: '提交成功',
            showCancel: false,
            success: res => {
              app.util.navigateTo('/cm_video_alarm/index/index');
            },
          });
        },
        fail: res => {
          wx.showModal({
            title: '提交失败',
            showCancel: false,
            success: res => {
              app.util.navigateTo('/cm_video_alarm/index/index');
            },
          });
        },
      });
    }
    if (isPass) {
      wx.showModal({
        title: hint,
        showCancel: false,
      });
    }
  },
  getUserInfo() {
    app.util.request({
      url: 'entry/wxapp/user',
      data: {
        op: 'getUser',
      },
      module: 'cm_video_alarm',
      success: res => {
        let userInfo = res.data.data;
        this.setData({
          userInfo,
        });
      },
    });
  },
});
